﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin.Accounts.Node
{
    public partial class Add : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindParentNode();
            }
        }

        private void BindParentNode()
        {
            List<Vincent.Permission.Model.account_nodeslist> lst = Vincent.Permission.Functions.Account.GetMenus("");// new BLL.account_nodeslist().GetModelList("");
            List<Vincent.Permission.Model.account_nodeslist> lstRoot = lst.Where(p => p.ParentID == "").OrderBy(p => p.OrderID).ToList();

            List<Vincent.Permission.Model.account_nodeslist> lstSource = new List<Vincent.Permission.Model.account_nodeslist>();
            foreach (Vincent.Permission.Model.account_nodeslist node in lstRoot)
            {
                lstSource.Add(node);
                ResetNode(lst, node, lstSource, 1);
            }

            BindDrowDownList(ddlParentNode, lstSource, "Text", "NodeID", true);
        }

        private void ResetNode(List<Vincent.Permission.Model.account_nodeslist> lstAll, Vincent.Permission.Model.account_nodeslist rootNode, List<Vincent.Permission.Model.account_nodeslist> lstSource, int level)
        {
            List<Vincent.Permission.Model.account_nodeslist> lstRoot = lstAll.Where(p => p.ParentID == rootNode.NodeID).OrderBy(p => p.OrderID).ToList();
            foreach (Vincent.Permission.Model.account_nodeslist node in lstRoot)
            {
                string startFlag = "";
                for (int i = 0; i < level; i++)
                {
                    startFlag = "--" + startFlag;
                }
                node.Text = startFlag + node.Text;

                lstSource.Add(node);
                ResetNode(lstAll, node, lstSource, level + 1);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtText.Text.Trim().Length == 0)
            {
                strErr += "Text不能为空！\\n";
            }

            if (!PageValidate.IsNumber(txtOrderID.Text))
            {
                strErr += "OrderID格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string Text = this.txtText.Text;
            string ParentID = ddlParentNode.SelectedValue;
            string Url = this.txtUrl.Text;
            string Description = this.txtDescription.Text;
            string ImgageUrl = "";
            string Target = "";
            int OrderID = int.Parse(this.txtOrderID.Text);

            Vincent.Permission.Model.account_nodeslist model = new Vincent.Permission.Model.account_nodeslist();
            model.NodeID =DBParameter.GetGuid();
            model.Text = Text;
            model.ParentID = ParentID;
            model.Url = Url;
            model.Description = Description;
            model.ImageUrl = ImgageUrl;
            model.Target = Target;
            model.OrderID = OrderID;

            Vincent.Permission.Functions.Account.AddMenu(model);
            MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}